package com.credit.mola.app.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.os.CountDownTimer
import android.view.LayoutInflater
import android.view.View
import com.credit.mola.app.R
import com.credit.mola.app.app.App
import com.credit.mola.app.databinding.DialogDataClearBinding
import com.credit.mola.app.pro.ProDialog
import com.credit.mola.app.utils.event.EventType
import com.credit.mola.app.utils.http.HttpHelper

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 删除用户数据dialog
 */
class DataClearDialog: ProDialog {
    private lateinit var binding: DialogDataClearBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""
    //等待时长
    private var countdownTimer: MyCountDownTimer? = null
    private var countdown = 10

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogDataClearBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.btnConfirm)
        setOnClickListener(R.id.imgClose)
        setOnClickListener(R.id.btnCancel)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if (v?.id == R.id.btnConfirm){
            mCallback?.onClick(v)
        }
    }

    /**
     * 设置提示图标
     */
    fun setTipsIconResource(resId: Int){
        binding.ivIcon.setImageResource(resId)
    }

    /**
     * 设置提示title
     */
    fun setTipsTitle(title: String){
        binding.tvTipsTitle.text = title
    }

    /**
     * 设置提示语
     */
    fun setTips(tips: String){
        binding.tvTips.text = tips
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    class MyCountDownTimer(private val dialog: DataClearDialog, millisInFuture: Int, countDownInterval: Long): CountDownTimer(millisInFuture.toLong(), countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            dialog.countdown = (millisUntilFinished / 1000).toInt()
            dialog.checkClearUserDataConfirmStatus()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            dialog.checkClearUserDataConfirmStatus()
        }
    }

    /**
     * 启动倒计时
     */
    @SuppressLint("SetTextI18n")
    private fun startCountdownTimer() {
        //随机 60秒 等待时长
        countdown = 10
        checkClearUserDataConfirmStatus()
        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this, countdown * 1000, 1000)
        }
        countdownTimer?.start()
    }

    @SuppressLint("SetTextI18n")
    private fun checkClearUserDataConfirmStatus(){
        if (countdown <= 0){
            binding.btnConfirm.isEnabled = true
            binding.btnConfirm.text = App.instance().getString(R.string.btn_confirm)
        } else {
            binding.btnConfirm.isEnabled = false
            binding.btnConfirm.text = "${App.instance().getString(R.string.btn_confirm)}(${countdown}s)"
        }
    }

    override fun onStart() {
        super.onStart()
        startCountdownTimer()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_USER_DATA_CLEAR_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_USER_DATA_CLEAR_DISMISS,eventRemark)
        if (countdownTimer != null){
            countdownTimer?.cancel()
            countdownTimer = null
        }
    }
}